package edu.tjhsst.androidIntranet.period8;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import edu.tjhsst.androidIntranet.R;
import edu.tjhsst.androidIntranet.db.IntranetDbAdapter;
import edu.tjhsst.androidIntranet.db.IntranetDbAdapter.EighthDbAdapter;

public class EighthAdapter extends BaseExpandableListAdapter {

	private static final String TAG = "EighthAdapter";
	private Context c;
	private DateFormat dateFormatter;
	IntranetDbAdapter.EighthDbAdapter eDbA;
	Cursor eighthCursor;
	Long endOffset=(long)1209600000;

	public EighthAdapter(Context c) {
		this.c=c;
		eDbA = IntranetDbAdapter.getEighthDbAdapter(c);
		reInitCursor();
		this.dateFormatter = SimpleDateFormat.getDateInstance();
		//put cheap database instantiation here
	}

	private void reInitCursor() {
		GregorianCalendar now=new GregorianCalendar();
		GregorianCalendar today=new GregorianCalendar(now.get(Calendar.YEAR),now.get(Calendar.MONTH),now.get(Calendar.DAY_OF_MONTH));
		eighthCursor = eDbA.getEighthCursor(today.getTimeInMillis(), today.getTimeInMillis()+endOffset);
	}
	
	public void loadNextTwoWeeks() {
		endOffset+=1209600000;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// gives block of the child
		eighthCursor.moveToFirst();
		long date=0;
		for(int i=0; i<=groupPosition; i++) {
			date=eighthCursor.getLong(0);
			while(eighthCursor.getLong(0)==date) {
				eighthCursor.moveToNext();
			}
		}
		for(int i=0; i<childPosition; i++) {
			eighthCursor.moveToNext();
		}
		Log.d(TAG, "Found Block: " + eighthCursor.getString(eighthCursor.getColumnIndex(EighthDbAdapter.KEY_BLOCK)));
		return eighthCursor.getString(eighthCursor.getColumnIndex(EighthDbAdapter.KEY_BLOCK));
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return groupPosition*20+childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.eighth_child_row, null);
		}
		TextView block = (TextView) v.findViewById(R.id.eighth_list_child_block);
		block.setText((String)getGroup(groupPosition));
		return v;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		eighthCursor.moveToFirst();
		long date=0;
		for(int i=0; i<=groupPosition; i++) {
			date=eighthCursor.getLong(0);
			while(eighthCursor.getLong(0)==date) {
				eighthCursor.moveToNext();
			}
		}
		int i=0;
		while(eighthCursor.getLong(0)==date) {
			eighthCursor.moveToNext();
			i++;
		}
		return i;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// gives date of the group
		eighthCursor.moveToFirst();
		long date=0;
		for(int i=0; i<=groupPosition; i++) {
			date=eighthCursor.getLong(0);
			while(eighthCursor.getLong(0)==date) {
				eighthCursor.moveToNext();
			}
		}
		Log.d(TAG, "Found Date: " + dateFormatter.format(new Date(eighthCursor.getLong(eighthCursor.getColumnIndex(EighthDbAdapter.KEY_DATE)))));
		return dateFormatter.format(new Date(eighthCursor.getLong(eighthCursor.getColumnIndex(EighthDbAdapter.KEY_DATE))));
	}

	@Override
	public int getGroupCount() {
		eighthCursor.moveToFirst();
		long date=0;
		int i=0;
		while(!eighthCursor.isAfterLast()) {
			if(eighthCursor.getLong(0)!=date) {
				date=eighthCursor.getLong(0);
				i++;
				eighthCursor.moveToNext();
			}
		}
		return i;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.eighth_group_row, null);
		}
		TextView date = (TextView) v.findViewById(R.id.eighth_list_group_date);
		date.setText((String)getGroup(groupPosition));
		return v;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
